package com.hk.web.manage.login.controller;

import com.hk.commons.base.BaseController;
import com.hk.commons.excel.ExcelUtil;
import com.hk.commons.utils.BeanConvertUtil;
import com.hk.commons.vo.ExecuteResult;
import com.hk.commons.vo.JsonResult;
import com.hk.dao.manage.user.qo.ManagerUserPageQO;
import com.hk.manage.login.excel.ManagerUserExport;
import com.hk.manage.login.excel.ManagerUserImport;
import com.hk.manage.login.service.ManagerLoginService;
import com.hk.web.manage.login.dto.ManageListPageDTO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/manager")
public class ManageController extends BaseController {
    @Resource
    private ManagerLoginService managerLoginService;

    /**
     * /manager/list
     */
    @GetMapping("/list")
    public JsonResult listPage(ManageListPageDTO dto){
        ManagerUserPageQO qo =BeanConvertUtil.copyObj(dto,ManagerUserPageQO.class);
        return copyToJsonResult(managerLoginService.listPage(qo));
    }


    /**
     * /manager/redis
     */
    @PostMapping("/redis")
    public JsonResult redis(){
        return copyToJsonResult(managerLoginService.redis());
    }

    /**
     * 导出
     * /manager/export
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        ExecuteResult<List<ManagerUserExport>> result = managerLoginService.getAll();
        List<ManagerUserExport> data = result.getData();
        ExcelUtil.exportExcel(data,"用户表头","用户sheet",ManagerUserExport.class,"用户",response);
    }

    /**
     * 导入
     * /manager/import
     */
    @PostMapping("/import")
    public JsonResult importData(@RequestParam("file") MultipartFile file){
        ExecuteResult<List<ManagerUserImport>> listExecuteResult = ExcelUtil.importExcel(file, 0, 1, ManagerUserImport.class);
        return copyToJsonResult(listExecuteResult);
    }
}
